import Vue from 'vue'
import Router from 'vue-router'
import Login from "./components/login.vue"
import home from "./components/home.vue"
import welcome from "./components/welcome.vue"
import Users from "./components/User/Users.vue"
Vue.use(Router)

const router = new Router({
  routes: [
    { path: "/", redirect: "/Login" },
    { path: "/Login", component: Login },
    { path: "/home", component: home, redirect: "welcome", 
    children: [{ path: "/welcome", component: welcome },
               { path: "/users", component: Users }
  ] }
  ]
})
router.beforeEach((to, from, next) => {
  //判断是否从登陆页面进来的
  if (to.path === "/login") return next()
  //判断登陆的时候是否携带了token
  const tokenStr = window.sessionStorage.getItem("token")
  //如果没有携带token则跳转到登录页面
  if (!tokenStr) return next("/login")
  next()
})
export default router
